﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers.world {
    import flash.events.*;
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.models.world.*;

    public class HugCommand extends Command {

        public var event:InfoEvent;
        public var world:WorldModel;
        public var userModel:UserModel;
        public var sunModel:SunModel;
        public var languageModel:LanguageModel;
        public var sessionModel:SessionModel;
        public var tickModel:TickModel;

        override public function execute():void{
            var _local2:int;
            var _local3:RequestEvent;
            l(("execute: " + this.execute));
            var _local1:IHugging = (this.world.getActor(this.event.id) as IHugging);
            if (!this.isFull){
                _local2 = ((_local1.blockedAt)<=this.tickModel.timestamp) ? this.tickModel.timestamp : _local1.blockedAt;
                _local2 = (_local2 + (this.sessionModel.doctorMealDuration - 10));
                _local1.hug(_local2);
                log("c hug");
                _local1.pendingHugs++;
                _local3 = new RequestEvent(EventConstants.SEND_REQUEST, this.onResult);
                _local3.addRequest("hug", {id:this.event.id});
                _local3.errorHandler = this.onError;
                dispatch(_local3);
            } else {
                dispatch(new Event(EventConstants.HANDLE_NEXT_AVATAR_ACTION));
            };
        }
        private function onResult(_arg1:ResultVO):void{
            var _local2:Object;
            var _local3:Object;
            var _local4:IHugging;
            var _local5:IRoom;
            if (_arg1.success){
                _local2 = _arg1.data.hug;
                _local3 = _local2.pet;
                _local4 = (this.world.getActor(this.event.id) as IHugging);
                _local5 = (this.world.getRoomById(_local4.roomId) as IRoom);
                _local4.pendingHugs--;
                if (_local4.pendingHugs <= 0){
                    _local4.blockedAt = _local3[ProtocolConstants.HUNGRY_AT];
                };
                dispatch(new InfoEvent(EventConstants.SET_BABY_TREATMENT, _local4.id));
            };
            dispatch(new Event(EventConstants.HANDLE_NEXT_AVATAR_ACTION));
        }
        private function onError(_arg1:String, _arg2:ResultVO):void{
            var _local3:IHugging = (this.world.getActor(this.event.id) as IHugging);
            _local3.pendingHugs--;
            dispatch(new Event(EventConstants.HANDLE_NEXT_AVATAR_ACTION));
            dispatch(new InfoEvent(EventConstants.ON_LOAD_ERROR, _arg1, _arg2.status));
        }
        private function get hugCount():int{
            var _local1:IHugging = (this.world.getActor(this.event.id) as IHugging);
            var _local2:int = (_local1.blockedAt - this.tickModel.timestamp);
            var _local3:Number = Math.ceil((_local2 / this.sessionModel.doctorMealDuration));
            return (_local3);
        }
        private function get isFull():Boolean{
            var _local1 = (this.hugCount >= GameConstants.DOCTOR_MEAL_COUNT);
            l(("result: " + _local1));
            return (_local1);
        }

    }
}//package net.wooga.pets.controllers.world 
